package edu.xlh.travel.dao;

import edu.xlh.travel.pojo.User;
import org.apache.ibatis.annotations.Param;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface UserDao  extends JpaRepository<User,String>, JpaSpecificationExecutor<User> {
    User findByMobile(String login);

    User findByEmail(String email);

    List<User> findByName(String login);

    @Modifying
    @Query(value = "update t_user set " +
            "name = CASE WHEN :#{#user.name} IS NULL THEN name ELSE :#{#user.name} END," +
            "email = CASE WHEN :#{#user.email} IS NULL THEN email ELSE :#{#user.email} END," +
            "mobile = CASE WHEN :#{#user.mobile} IS NULL THEN mobile ELSE :#{#user.mobile} END," +
            "sex = CASE WHEN :#{#user.sex} IS NULL THEN sex ELSE :#{#user.sex}  END," +
            "perm = CASE WHEN :#{#user.perm} IS NULL THEN perm ELSE :#{#user.perm}  END," +
            "age= CASE WHEN  :#{#user.age} IS NULL THEN age ELSE :#{#user.age} END " +
            "where id =:#{#user.id}",nativeQuery = true )
    int updateUserByUserId(@Param("user")User user);

    @Transactional
    @Modifying
    @Query(value = "update User set isVip =?2 where id =?1")
    void updateIsVipByUserId(@Param("userId") String userId , @Param("isVip") String isVip);

}
